<?php
namespace Depart\Controller;
use Think\Controller;
// use Common\Controller\AdminBaseController;

Class ProdController extends Controller{
	public function prodinfo(){
		$get=I('get.');

		if($get){
			//带条件查询
//dump($get);die;
			if($get['start']){
			$get['start']=array('exp','is null');
			}
		$data=D('prod')->relation(TRUE)->where($get)->select();
		}else{
			$data=D('prod')->relation(TRUE)->select();
		}
		$this->assign('prod',$data);
		$this->display();
	}
	public function add_prod(){
		$getdata=I('post.');
		
		if($getdata){
			if($getdata['prodcode']){//排除没有输入产品编号或批次的
				$data['prodbatch']=$getdata['prodcode'];
				$data['qty']=$getdata['qty'];
				$map['spec']=$getdata['spec'];
				$map['type']=$getdata['type'];
				$result=M('dwg')->where($map)->find();
				if($result['id'] or empty($map['spec'])){//排除图纸未登记或登记类别不一致的，不输型号也可以
					$data['dwgid']=$result['id'];
					$data['type']=$getdata['type'];
					$i=(($data['qty'] == '1' )and ($getdata['type'] <>'5'));
					$j=(($data['qty'] > '1') and (strlen($data['prodbatch'])==9)and ($getdata['type'] =='5'));
					// echo $i;echo $j;
					 // dump($data);die;
					dump($result);die;

					//气瓶按批输入，其他产品只能一台一输入
					if((($data['qty'] == 1 ) and ($data['type'] <>5) ) or (($data['qty'] <> 1) and (strlen($data['prodbatch'])==10) and ($data['type'] ==5))){
						//dump($data);die;
				        $result2=D('prod_progress')->add($data);
				        if ($result2) {//在批次中导入后再在产品表中导入
				        	$prod['batchid']=$result2['id'];
				        	if($getdata['qty'] ==1){
				        		$prod['prodcode']=$getdata['prodcode'];
				        		$result1=D('prod_info')->add($prod);
				        	}else{
				        		for ($i=0;$i<$getdata['qty'];++$i){
				        			$prod['prodcode']=$getdata['prodcode']."-".substr(101+$i,1,2);
				        			$result1=D('prod_info')->add($prod);
				        		}
				        	}
				            $this->success('登记完成',U('Depart/Prod/prodinfo'));
				        }else{
				            $this->error('登记失败，可能该产品已登记');
				        }
			        }else{
			        	$this->error('除气瓶以外，一次只能登记一台产品');
			        }
		        }else{
		        	$this->error('规格选型不正确');
		        }
	        }else{
	        	$this->error('未填写产品编号或批号');
	        }
        }else{
        	$map['type']=D('type')->where('type="产品分类"')->select();
        	$this->assign('map',$map);
        	$this->display();
        }
	}
	public function checkdate(){
		$get=I('get.');
		$map['id']=$get['id'];
		$data[$get['item']]=time();
		$result=D('prod_progress')->where($map)->save($data);
		if($result){
			$this->success('确认完成',U('Depart/Prod/prodinfo'));
		}
		//dump($data);
	}

	public function prod_dwg(){
		$post=I('post.');
		$map['type']=$post['type'];
		$map['dwgcode']=$post['dwgcode'];
		//dump($map);
		$map1['id']=$post['id'];
		$result=D('dwg')->where($map)->select();
		if($result){
			$data['dwgid']=$result[0]['id'];
			$re=D('prod_progress')->where($map1)->save($data);
			$this->success('确认完成',U('Depart/Prod/prodinfo'));
		}else{
			$this->error('图纸未登记或分类不对',U('Depart/Dwg/dwginfo'));
		}
		//dump($result);
	}

	public function prod_plan(){
		$get=I('post.');
		// dump($post);die;
		$map['id']=$get['id'];
		$data[$get['item']]=strtotime($get['date']);
		$result=D('prod_progress')->where($map)->save($data);
		if($result){
			$this->success('确认完成',U('Depart/Prod/prodinfo'));
		}
	}
	public function prodprogress(){

	}
}